package pessoas
{
	import org.flixel.FlxPoint;
	import org.flixel.FlxState;
	import org.flixel.FlxSprite;
	import org.flixel.FlxG;
	
	/**
	 * ...
	 * @author Lucas Mello
	 */
	public class Policial extends PessoaCacadora
	{
		[Embed(source = '../../assets/policialHead.png')] public static var head_img:Class;
		[Embed(source = '../../assets/policialBody.png')] public static var body_img:Class;
		[Embed(source = '../../assets/policialLegs2.png')] public static var legs_img:Class;
		[Embed(source = '../../assets/policialArms.png')] public static var arms_img:Class;
		
		[Embed(source = "../../assets/som/whistle.mp3")]
		public static var attentionSound:Class;
		
		[Embed(source = "../../assets/som/you_there.mp3")]
		public static var attentionSound2:Class;
		
		public var ativo:Boolean = true;
		
		public function Policial()
		{
			super();
		}
		
		public function respawn(state:MainState, pos:FlxPoint, dir:int):void
		{
			init(state, pos, dir, head_img, body_img, legs_img, arms_img);
			
			mainState = state;
			
			hasChased = false;
			chasing = false;
		}
		
		protected override function chase():void
		{
			var dist:Number;
			if (!chasing)
			{	
				if (!hasChased)
				{
					var playerPos:FlxPoint = new FlxPoint(mainState.salazar.x, mainState.salazar.y);
					dist = Math.sqrt((playerPos.x - x) * (playerPos.x - x) + (playerPos.y - y) * (playerPos.y - y));
					
					if (dist < noticeRadius && mainState.playerController.isSucking)
					{
						if (Math.random() > 0.8)
							FlxG.play(attentionSound2);
							else
							FlxG.play(attentionSound);
						beginChaseAnimation();
						chasing = true;
						chasePos = playerPos;
						chaseVelocity = new FlxPoint(playerPos.x - x, playerPos.y - y);
						chaseVelocity.x *= chaseSpeed / dist;
						chaseVelocity.y *= chaseSpeed / dist;
						
						keepVelocity.x = velocity.x;
						keepVelocity.y = velocity.y;
						
						velocity.x = 0;
						velocity.y = 0;
					}
				}
			}
			else
			{
				x += chaseVelocity.x;
				y += chaseVelocity.y;
				
				dist = Math.sqrt((chasePos.x - x) * (chasePos.x - x) + (chasePos.y - y) * (chasePos.y - y));
				if (dist < chaseMinDist)
				{
					chasing = false;
					hasChased = true;
					
					velocity.x = keepVelocity.x;
					velocity.y = keepVelocity.y;
				}
			}
		}
		
		override public function update():void 
		{
			super.update();
			
			chase();
		}
	}
}